﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MusicOnLine.Models;
using MusicOnLine.Common;

namespace MusicOnLine.Controllers
{
    [MOLAuthorzie(Roles = "Admin")]
    public class ManageSingerController : Controller
    {
        private MusicPlayEntities db = new MusicPlayEntities();

        //
        // GET: /ManageSinger/

        public ViewResult Index()
        {
            return View();
        }
        public ActionResult QueryAtists(string g, int PageIndex)
        {
            string s = HttpUtility.UrlDecode(g, System.Text.Encoding.GetEncoding("utf-8"));
            var obje = db.Artists.Where(o => o.ArtistName.Contains(s)).OrderByDescending(o=>o.CreateDate);
            PageList<Artist> model = new PageList<Artist>(obje, PageIndex, 15);
            var json = Json(model, JsonRequestBehavior.AllowGet);

            return json;
        }

        public ActionResult QuickSearchArtist(string term)
        {
            var model = db.Artists.Where(o => o.ArtistName.Contains(term)).Select(o => new { value = o.ArtistName }).Take(10).ToList();
            return Json(model, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /ManageSinger/Details/5

        public ViewResult Details(int id)
        {
            Artist artist = db.Artists.Find(id);
            return View(artist);
        }

        //
        // GET: /ManageSinger/Create

        //public ActionResult Create()
        //{
        //    return View();
        //}

        //
        // POST: /ManageSinger/Create

        //[HttpPost]
        //public ActionResult Create(Artist artist)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Artists.Add(artist);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    return View(artist);
        //}

        //
        // GET: /ManageSinger/Edit/5

        public ActionResult Edit(int id)
        {
            Artist artist = db.Artists.Find(id);
            return View(artist);
        }

        // POST: /ManageSinger/Edit/5

        [HttpPost]
        public ActionResult Edit(Artist artist)
        {
            if (ModelState.IsValid)
            {
                string username = HttpContext.User.Identity.Name;
                var file = Request.Files["fileImgUrl"];
                if (file.FileName != "")
                {                   
                    string ex = System.IO.Path.GetExtension(file.FileName);
                    string fileName = Guid.NewGuid().ToString() + ex;
                    string path = Server.MapPath("~/SingerImg/");
                    artist.ImgUrl = fileName;
                    file.SaveAs(path + fileName);
                }
                artist.ModifyBy = username;
                artist.ModifyDate = DateTime.Now;
                db.Entry(artist).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", "ManageSinger", new { id=artist.ID});
            }
            return View(artist);
        }

        //
        // GET: /ManageSinger/Delete/5

        [MOLAuthorzie(Roles = "SuperAdmin")]
        public ActionResult Delete(int id)
        {
            Artist artist = db.Artists.Find(id);
            return View(artist);
        }

        //
        // POST: /ManageSinger/Delete/5
        [MOLAuthorzie(Roles = "SuperAdmin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Artist artist = db.Artists.Find(id);
            db.Artists.Remove(artist);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}